﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.Plumbing.Properties;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	public partial class MainForm : System.Windows.Forms.Form
	{
		public event HighLightElementHandler HighLightElement;

		public MainForm(Autodesk.Revit.DB.Document doc, UIDocument uiDoc, HYSystemCal hySystemCal)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.Icon = (base.Icon = Resource.YArchitechIcon);
			this.doc = doc;
			this.uiDoc = uiDoc;
			this.hySystemCal = hySystemCal;
			this.dh = new DataOperateHelper(uiDoc);
			this.dh.SysType = hySystemCal.systemType;
			IList<string> listHeader = null;
			switch (hySystemCal.systemType)
			{
			case HYSystemCal.SystemType.Drainage:
				listHeader = this.listHeaderDrainage;
				this.diameterRow = 6;
				break;
			case HYSystemCal.SystemType.Feed:
				listHeader = this.listHeaderFeed;
				this.diameterRow = 6;
				break;
			case HYSystemCal.SystemType.Fire:
				this.treeView.Visible = false;
				this.toolDesignCalc.Text = "计算";
				this.toolCheckCalc.Visible = false;
				this.dataGridView.Location = new System.Drawing.Point(7, 45);
				this.dataGridView.Size = new Size(988, 590);
				this.lbsring.Visible = true;
				listHeader = this.listHeaderFire;
				this.diameterRow = 5;
				break;
			}
			this.dh.BindDataGridHeader(listHeader, this.dataGridView);
			this.hyTwinkle = new HYTwinkle();
			this.hyTwinkle.View = uiDoc.ActiveView;
			this.HighLightElement += this.OnHighLightElement;
		}

		private void InitTreeView(HYSystemCal hySystemCal)
		{
			if (hySystemCal.MainPipeCals.Count <= 0)
			{
				return;
			}
			if (this.treeView.Nodes != null)
			{
				this.treeView.Nodes.Clear();
			}
			this.levelNum = 0;
			this.listLpipe = new List<HYPipeCal>();
			foreach (HYPipeCal hypipeCal in hySystemCal.MainPipeCals)
			{
				if (hypipeCal.Number.Contains("F-"))
				{
					this.levelNum++;
				}
				if (hypipeCal.Number.Contains("L-") && hySystemCal.PipeCals.Contains(hypipeCal))
				{
					this.listLpipe.Add(hypipeCal);
				}
			}
			TreeNode treeNode = new TreeNode();
			treeNode.Text = "水系统";
			this.treeView.Nodes.Add(treeNode);
			if (this.listLpipe.Count > 0)
			{
				TreeNode treeNode2 = new TreeNode();
				treeNode2.Text = "立管";
				treeNode.Nodes.Add(treeNode2);
			}
			for (int i = 1; i <= this.levelNum; i++)
			{
				TreeNode treeNode3 = new TreeNode();
				treeNode3.Text = "楼层" + i.ToString();
				treeNode.Nodes.Add(treeNode3);
			}
			this.treeView.ExpandAll();
		}

		private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
		{
			this.treeView.SelectedNode.BackColor = System.Drawing.Color.DodgerBlue;
			this.treeView.SelectedNode.ForeColor = System.Drawing.Color.White;
			this.UpdataDataFormDataView();
			if (e.Node.Parent == null)
			{
				if (this.dataGridView.Rows != null)
				{
					this.dataGridView.Rows.Clear();
				}
				this.dh.BindDataGridData(this.dataGridView, this.hySystemCal, this.isAllData);
				return;
			}
			if (e.Node.Text == "立管")
			{
				if (this.dataGridView.Rows != null)
				{
					this.dataGridView.Rows.Clear();
				}
				this.dh.BindDataGridDataByList(this.dataGridView, this.listLpipe);
				return;
			}
			int num = e.Node.Text.Remove(0, 2).ToInt(0);
			for (int i = 1; i <= this.levelNum; i++)
			{
				if (num == i)
				{
					this.listHpipe = new List<HYPipeCal>();
					foreach (HYPipeCal hypipeCal in this.hySystemCal.PipeCals)
					{
						if (hypipeCal.Number.Equals("F-" + i.ToString()) || hypipeCal.Number.Contains("F-" + i.ToString() + "-"))
						{
							this.listHpipe.Add(hypipeCal);
						}
					}
					if (this.listHpipe.Count > 0)
					{
						if (this.dataGridView.Rows != null)
						{
							this.dataGridView.Rows.Clear();
						}
						this.dh.BindDataGridDataByList(this.dataGridView, this.listHpipe);
					}
				}
			}
		}

		private void treeView_BeforeSelect(object sender, TreeViewCancelEventArgs e)
		{
			if (this.treeView.SelectedNode != null)
			{
				this.treeView.SelectedNode.BackColor = System.Drawing.Color.White;
				this.treeView.SelectedNode.ForeColor = System.Drawing.Color.Black;
			}
		}

		private void StopTwinkle()
		{
			this.hyTwinkle.Stop();
		}

		private void OnHighLightElement(object sender, ElementEventArgs e)
		{
			if (this.hyTwinkle == null)
			{
				return;
			}
			this.hyTwinkle.Stop();
			ElementId elementId = new ElementId(e.ElementId);
			List<ElementId> list = new List<ElementId>();
			if (this.doc.GetElementById(elementId) != null)
			{
				list.Add(elementId);
			}
			this.hyTwinkle.Ids = list;
			this.hyTwinkle.Start();
		}

		public void RefreshTheHYSystemCal(HYSystemCal hySystemCal)
		{
			this.hySystemCal = hySystemCal;
		}

		protected override void WndProc(ref Message m)
		{
			try
			{
				if (m.WParam.ToInt32() == 61472)
				{
					this.beMinimize = true;
					if (this.hyTwinkle != null)
					{
						this.hyTwinkle.Stop();
						this.ElemIds = new List<ElementId>();
						for (int i = 0; i < this.dataGridView.SelectedRows.Count; i++)
						{
							ElementId elementId = new ElementId((this.dataGridView.SelectedRows[i].Cells[0].Value as HYPipeCal).Id);
							if (this.doc.GetElementById(elementId) != null)
							{
								this.ElemIds.Add(elementId);
							}
						}
						base.DialogResult = DialogResult.Abort;
					}
				}
				else
				{
					base.WndProc(ref m);
				}
			}
			catch (Exception)
			{
			}
		}

		protected override void OnClosed(EventArgs e)
		{
			base.OnClosed(e);
			if (this.hyTwinkle != null)
			{
				this.hyTwinkle.Dispose();
			}
		}

		private void MainForm_Load(object sender, EventArgs e)
		{
			this.cmb_PipeInfo.SelectedIndexChanged += this.cmb_PipeInfo_SelectedIndexChanged;
			this.cmb_PipeInfo.TextChanged += this.cmb_PipeInfo_TextChanged;
			this.dataGridView.Controls.Add(this.cmb_PipeInfo);
			this.cmb_PipeInfo.Visible = false;
			this.toolAllData.Visible = false;
			this.toolPartData.Visible = false;
			if (this.hySystemCal.systemType == HYSystemCal.SystemType.Drainage || this.hySystemCal.systemType == HYSystemCal.SystemType.Feed)
			{
				this.InitTreeView(this.hySystemCal);
			}
			if (this.dataGridView.Rows != null)
			{
				this.dataGridView.Rows.Clear();
				this.dh.BindDataGridData(this.dataGridView, this.hySystemCal, this.isAllData);
			}
			if (this.hySystemCal.systemType == HYSystemCal.SystemType.Feed)
			{
				this.SetBathroomPer();
			}
		}

		private void cmb_PipeInfo_TextChanged(object sender, EventArgs e)
		{
			this.dataGridView.CurrentCell.Value = ((System.Windows.Forms.ComboBox)sender).Text;
		}

		private void cmb_PipeInfo_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.dataGridView.CurrentCell.Value = ((System.Windows.Forms.ComboBox)sender).Text;
		}

		private void toolExtract_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Abort;
		}

		private void toolReturn_Click(object sender, EventArgs e)
		{
			if (this.isCalculated)
			{
				this.UpdataDataFormDataView();
				base.DialogResult = DialogResult.OK;
				return;
			}
			YJKMessageBox.Information("还没有进行计算，计算以后才能赋回图面");
		}

		private void UpdataDataFormDataView()
		{
			List<HYPipeCal> list = new List<HYPipeCal>();
			for (int i = 0; i < this.dataGridView.Rows.Count; i++)
			{
				HYPipeCal hypipeCal = new HYPipeCal();
				hypipeCal = (this.dataGridView.Rows[i].Cells[0].Value as HYPipeCal);
				hypipeCal.nDN = int.Parse(this.dataGridView.Rows[i].Cells[this.diameterRow].Value.ToString());
				list.Add(hypipeCal);
			}
			using (List<HYPipeCal>.Enumerator enumerator = list.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					HYPipeCal item = enumerator.Current;
					if (this.hySystemCal.PipeCals.Exists((HYPipeCal q) => q.Id == item.Id))
					{
						this.hySystemCal.PipeCals.First((HYPipeCal q) => q.Id == item.Id).nDN = item.nDN;
					}
					if (this.hySystemCal.MainPipeCals.Exists((HYPipeCal q) => q.Id == item.Id))
					{
						this.hySystemCal.MainPipeCals.First((HYPipeCal q) => q.Id == item.Id).nDN = item.nDN;
					}
				}
			}
		}

		private void toolSystem_Click(object sender, EventArgs e)
		{
			if (this.hySystemCal.systemType == HYSystemCal.SystemType.Drainage)
			{
				FormParasSetting formParasSetting = new FormParasSetting();
				if (formParasSetting.ShowDialog() == DialogResult.OK)
				{
					this.hySystemCal.m_calDrain = formParasSetting.calDrainData;
					this.hySystemCal.m_calParameter = formParasSetting.hyCalParameter;
					return;
				}
			}
			else
			{
				if (this.hySystemCal.systemType == HYSystemCal.SystemType.Feed)
				{
					FormSupplyParasSetting formSupplyParasSetting = new FormSupplyParasSetting(this.doc, this.hySystemCal.Root as HYPipeCal);
					if (formSupplyParasSetting.ShowDialog() == DialogResult.OK)
					{
						this.hySupplyCal.supplyFormula = formSupplyParasSetting.hySupplyFormula;
						this.hySupplyCal.calParameter = formSupplyParasSetting.hyCalParameter;
						this.hySystemCal.supplyParameter = formSupplyParasSetting.hySupplyFormula;
						this.hySystemCal.m_calParameter = formSupplyParasSetting.hyCalParameter;
						this.hySupplyCal.dQuotLoss = formSupplyParasSetting.djz;
					}
					this.szBuildTypeOld = formSupplyParasSetting.szBuildTypeOld;
					return;
				}
				if (this.hySystemCal.systemType == HYSystemCal.SystemType.Fire)
				{
					FormProtectRadiusCalc formProtectRadiusCalc = new FormProtectRadiusCalc();
					if (formProtectRadiusCalc.ShowDialog() == DialogResult.OK)
					{
						this.hySystemCal.hyFireData = formProtectRadiusCalc.hyFireData;
					}
				}
			}
		}

		private void SetBathroomPer()
		{
			FormSupplyParasSetting formSupplyParasSetting = new FormSupplyParasSetting(this.doc, this.hySystemCal.Root as HYPipeCal);
			if (string.IsNullOrEmpty(this.szBuildTypeOld))
			{
				formSupplyParasSetting.UpdataBathroomPer(this.szBuildTypeOld);
			}
			this.szBuildTypeOld = formSupplyParasSetting.szBuildTypeOld;
			this.hySupplyCal.supplyFormula = formSupplyParasSetting.hySupplyFormula;
			this.hySupplyCal.calParameter = formSupplyParasSetting.hyCalParameter;
			this.hySystemCal.supplyParameter = formSupplyParasSetting.hySupplyFormula;
			this.hySystemCal.m_calParameter = formSupplyParasSetting.hyCalParameter;
			this.hySupplyCal.dQuotLoss = formSupplyParasSetting.djz;
			formSupplyParasSetting.Close();
		}

		private void toolDesignCalc_Click(object sender, EventArgs e)
		{
			if (this.hySystemCal.systemType == HYSystemCal.SystemType.Drainage)
			{
				this.hySystemCal.CalDrain(true);
				if (this.dataGridView.Rows != null)
				{
					this.dataGridView.Rows.Clear();
				}
				this.dh.BindDataGridData(this.dataGridView, this.hySystemCal, this.isAllData);
				this.isCalculated = true;
				return;
			}
			if (this.hySystemCal.systemType == HYSystemCal.SystemType.Feed)
			{
				this.hySupplyCal.CalSystem(this.hySystemCal, true);
				if (this.dataGridView.Rows != null)
				{
					this.dataGridView.Rows.Clear();
				}
				this.dh.BindDataGridData(this.dataGridView, this.hySystemCal, this.isAllData);
				this.isCalculated = true;
				return;
			}
			if (this.hySystemCal.systemType == HYSystemCal.SystemType.Fire)
			{
				this.hyHydrantCal.Cal(this.hySystemCal);
				if (this.dataGridView.Rows != null)
				{
					this.dataGridView.Rows.Clear();
					this.dh.BindDataGridData(this.dataGridView, this.hySystemCal, this.isAllData);
				}
				this.isCalculated = true;
				this.lbsring.Text = this.hySystemCal.ShowResults;
			}
		}

		private void toolCheckCalc_Click(object sender, EventArgs e)
		{
			this.UpdataDataFormDataView();
			if (this.hySystemCal.systemType == HYSystemCal.SystemType.Drainage)
			{
				this.hySystemCal.CalDrain(false);
				if (this.dataGridView.Rows != null)
				{
					this.dataGridView.Rows.Clear();
					this.dh.BindDataGridData(this.dataGridView, this.hySystemCal, this.isAllData);
					return;
				}
			}
			else
			{
				if (this.hySystemCal.systemType == HYSystemCal.SystemType.Feed)
				{
					this.hySupplyCal.CalSystem(this.hySystemCal, false);
					if (this.dataGridView.Rows != null)
					{
						this.dataGridView.Rows.Clear();
						this.dh.BindDataGridData(this.dataGridView, this.hySystemCal, this.isAllData);
					}
					this.isCalculated = true;
					return;
				}
				if (this.hySystemCal.systemType == HYSystemCal.SystemType.Fire)
				{
					this.lbsring.Text = this.hySystemCal.ShowResults;
				}
			}
		}

		private void toolExcel_Click(object sender, EventArgs e)
		{
			HVACUserSetting hvacuserSetting = new HVACUserSetting();
			this.hySystemCal.hyFireData = hvacuserSetting.GetProtectRadiusSet();
			new CalExportExcelProgressBar(this.hySystemCal, null).ShowDialog();
		}

		private void BindingComboBoxInfo(List<string> ItemContants, bool bEdit = false)
		{
			if (this.cmb_PipeInfo == null)
			{
				this.cmb_PipeInfo = new System.Windows.Forms.ComboBox();
			}
			DataTable dataTable = new DataTable();
			dataTable.Columns.Add("Value");
			dataTable.Columns.Add("Contants");
			for (int i = 0; i < ItemContants.Count; i++)
			{
				DataRow dataRow = dataTable.NewRow();
				dataRow[0] = i;
				dataRow[1] = ItemContants[i];
				dataTable.Rows.Add(dataRow);
			}
			this.cmb_PipeInfo.ValueMember = "Value";
			this.cmb_PipeInfo.DisplayMember = "Contants";
			this.cmb_PipeInfo.DataSource = dataTable;
			if (bEdit)
			{
				this.cmb_PipeInfo.DropDownStyle = ComboBoxStyle.DropDown;
				return;
			}
			this.cmb_PipeInfo.DropDownStyle = ComboBoxStyle.DropDownList;
		}

		public void LoadComboBoxToCurrendCell(string str)
		{
			System.Drawing.Rectangle cellDisplayRectangle = this.dataGridView.GetCellDisplayRectangle(this.dataGridView.CurrentCell.ColumnIndex, this.dataGridView.CurrentCell.RowIndex, false);
			this.cmb_PipeInfo.Text = str;
			this.cmb_PipeInfo.Left = cellDisplayRectangle.Left;
			this.cmb_PipeInfo.Top = cellDisplayRectangle.Top;
			this.cmb_PipeInfo.Width = cellDisplayRectangle.Width;
			this.cmb_PipeInfo.Height = cellDisplayRectangle.Height;
			this.cmb_PipeInfo.Visible = true;
		}

		private string GetCloserNum(string sNum, List<double> snewNums)
		{
			if (snewNums.Count == 0)
			{
				return null;
			}
			int num = (int)short.Parse(sNum);
			double num2 = snewNums[0];
			double num3 = Math.Abs((double)num - num2);
			for (int i = 0; i < snewNums.Count; i++)
			{
				if (Math.Abs((double)num - snewNums[i]) <= num3)
				{
					num3 = Math.Abs((double)num - snewNums[i]);
					if (num2 < snewNums[i])
					{
						num2 = snewNums[i];
					}
				}
			}
			return num2.ToString();
		}

		private void dataGridView_CurrentCellChanged(object sender, EventArgs e)
		{
			if (this.dataGridView.CurrentCell != null)
			{
				if (this.dataGridView.CurrentCell.ColumnIndex == this.diameterRow)
				{
					HYPipeCal hypipeCal = this.dataGridView.CurrentRow.Cells[0].Value as HYPipeCal;
					List<string> list = new List<string>();
					List<double> list2 = new List<double>();
					foreach (KeyValuePair<int, double> keyValuePair in hypipeCal.dirDNArray)
					{
						list.Add(keyValuePair.Key.ToString());
						list2.Add((double)keyValuePair.Key);
					}
					string closerNum = this.GetCloserNum(this.dataGridView.CurrentCell.Value.ToString(), list2);
					this.BindingComboBoxInfo(list, false);
					this.LoadComboBoxToCurrendCell(closerNum);
					return;
				}
				this.cmb_PipeInfo.Visible = false;
			}
		}

		private void toolAllData_Click(object sender, EventArgs e)
		{
			if (!this.isAllData)
			{
				this.isAllData = true;
				if (this.dataGridView.Rows != null)
				{
					this.UpdataDataFormDataView();
					this.dataGridView.Rows.Clear();
					this.dh.BindDataGridData(this.dataGridView, this.hySystemCal, this.isAllData);
				}
			}
		}

		private void toolPartData_Click(object sender, EventArgs e)
		{
			if (this.isAllData)
			{
				this.isAllData = false;
				if (this.dataGridView.Rows != null)
				{
					this.UpdataDataFormDataView();
					this.dataGridView.Rows.Clear();
					this.dh.BindDataGridData(this.dataGridView, this.hySystemCal, this.isAllData);
				}
			}
		}

		private void button1_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void dataGridView_SelectionChanged(object sender, EventArgs e)
		{
			this.ElemIds = new List<ElementId>();
			if (this.dataGridView.SelectedRows.Count > 0)
			{
				ElementId elementId = new ElementId((this.dataGridView.SelectedRows[0].Cells[0].Value as HYPipeCal).Id);
				this.HighLightElement(this, new ElementEventArgs(elementId.IntegerValue));
				if (this.doc.GetElementById(elementId) != null)
				{
					this.ElemIds.Add(elementId);
				}
			}
		}

		private IList<string> listHeaderDrainage = new string[]
		{
			"管道对象",
			"管段编号",
			"坡度",
			"流量(L/s)",
			"流速(m/s)",
			"长度(m)",
			"公称直径(mm)",
			"管道充满度",
			"当量"
		};

		private IList<string> listHeaderFeed = new string[]
		{
			"管道对象",
			"管段编号",
			"流量(L/s)",
			"流速(m/s)",
			"水力坡降(mH2O/m)",
			"长度(m)",
			"公称直径(mm)",
			"当量",
			"沿程阻力(m)"
		};

		private IList<string> listHeaderFire = new string[]
		{
			"管道对象",
			"管段编号",
			"流量(L/s)",
			"流速(m/s)",
			"长度(m)",
			"公称直径(mm)",
			"水力坡降(kPa/m)",
			"沿程损失(kPa)"
		};

		private Document doc;

		private UIDocument uiDoc;

		public HYSystemCal hySystemCal = new HYSystemCal();

		private HYSupplyCal hySupplyCal = new HYSupplyCal();

		private HYHydrantCal hyHydrantCal = new HYHydrantCal();

		private System.Windows.Forms.ComboBox cmb_PipeInfo = new System.Windows.Forms.ComboBox();

		public DataOperateHelper dh;

		private bool isAllData = true;

		private bool isCalculated;

		private List<HYPipeCal> listLpipe;

		private List<HYPipeCal> listHpipe;

		private int levelNum;

		private int diameterRow = 6;

		private string szBuildTypeOld = "";

		private HYTwinkle hyTwinkle;

		private const int SC_MINIMIZE = 61472;

		public bool beMinimize;

		public List<ElementId> ElemIds;
	}
}
